package com.service.impl;

import com.dao.FoodsDao;
import com.dao.TypeDao;
import com.dao.impl.FoodsDaoImpl;
import com.dao.impl.TypeDaoImpl;
import com.entity.*;
import com.service.FoodsService;

import java.util.List;
import java.util.Map;

public class FoodsServiceImpl implements FoodsService {
    FoodsDao foodsDao=new FoodsDaoImpl();

    /**
     * 查询所有食物
     * @return
     */
    @Override
    public List<Foods> foodsFindAll() {
        List<Foods> foods= foodsDao.foodsFindAll();
        if (foods!=null){
            return foods;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> foodFindAll() {
        List<Map<String, Object>> foods= foodsDao.foodFindAll();
        if (foods!=null){
            return foods;
        }
        return null;
    }


    /**
     * 查询点赞数量
     * @return
     */
    @Override
    public List<Like> findLike() {
        List<Like> like= foodsDao.findLike();
        if(like!=null){
            return like;
        }
        return null;
    }

    @Override
    public List<Like> findUserLove() {
        List<Like> love= foodsDao.findUserLove();
        if(love!=null){
            return love;
        }
        return null;
    }

    /**
     * 查询三个点赞最多的食物
     * @return
     */
    @Override
    public List<Map<String, Object>> findThree() {
        List<Map<String, Object>> lovethree=foodsDao.findthree();
        if (lovethree!=null){
            return  lovethree;
        }
        return null;
    }

    @Override
    public int findFoodLike(int customerId, int foodId) {
        List<Like> food=foodsDao.findFoodLike(customerId,foodId);
        if (food != null && food.size () != 0){
            return 1;
        }
       return 0;
    }

    @Override
    public int foodGiveLike(int customerId, int foodId) {
        int num=foodsDao.foodGiveLike(customerId,foodId);
        if (num!=0){
            return num;
        }
        return 0;
    }

    @Override
    public int foodCancelLike(int customerId1, int foodId1) {
        int num=foodsDao.foodCancelLike(customerId1,foodId1);
        if (num!=0){
            return num;
        }
        return 0;
    }
    /**
     * 按照id查询食物信息
     * @param foodsId
     * @return
     */
    @Override
    public List<Map<String, Object>> findId(int foodsId) {
        List<Map<String, Object>> foods=foodsDao.findId(foodsId);
        if (foods!=null){
            return  foods;
        }
        return null;
    }

    /**
     * 按照食物id查询点赞数量
     * @param likeId
     * @return
     */
    @Override
    public List<Map<String, Object>> likeId(int likeId) {
        List<Map<String, Object>> likes=foodsDao.likeId(likeId);
        if (likes!=null){
            return  likes;
        }
        return null;
    }

    /**
     * 根据食物类型查询数据
     * @param typeId
     * @return
     */
    @Override
    public List<Map<String, Object>> typeId(int typeId) {
        List<Map<String, Object>> list=foodsDao.typeId(typeId);
        if (list!=null){
            return  list;
        }
        return null;
    }

    /**
     * 添加购物车
     * @param price
     * @param number
     * @param userId
     * @param foodId
     * @param size
     * @return
     */
    @Override
    public int updataCart(int price, int number, int userId, int foodId, int size,String fall) {
        int list=foodsDao.updataCart(price,number,userId,foodId,size,fall);
        if (list!=0){
            return  list;
        }
        return 0;
    }

    /**
     * 根据用户id查找购物车
     * @param userId
     * @return
     */
    @Override
    public List<Cart> findCart(int userId) {
        List<Cart> list=foodsDao.findCart(userId);
        if (list!=null){
            return  list;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> listCart(int price, int number, int userId, int foodId, int size) {
        List<Map<String, Object>> list=foodsDao.listCart(price,number,userId,foodId,size);
        if (list!=null){
            return  list;
        }
        return null;
    }

    @Override
    public int updetaNumber(int listCartId, int number) {
        int list=foodsDao.updetaNumber(listCartId,number);
        if (list!=0){
            return  list;
        }
        return 0;
    }

    @Override
    public int removeCart(int listCartId) {
        int list=foodsDao.removeCart(listCartId);
        if (list!=0){
            return  list;
        }
        return 0;
    }

    @Override
    public int deleteFood(int id) {
        int i = foodsDao.deleteFood(id);
        if(i>0){
            return i;
        }else {
            return 0;
        }

    }

    @Override
    public int modify(Foods foods) {
        int modify = foodsDao.modify(foods);
        return modify;
    }

    @Override
    public int add(String name, String photo, int price, String describe, int type_id) {
        int add = foodsDao.add(name, photo, price, describe, type_id);
        return add;
    }

    @Override
    public List<Map<String, Object>> bgFoodsFindAll(int pageIndex, int pageSize) {
        List<Map<String, Object>> foods= foodsDao.bgFoodsFindAll(pageIndex,pageSize);
        if (foods!=null){
            return foods;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> bgFoodGetByID(int foodId) {
        List<Map<String, Object>> foods= foodsDao.bgFoodGetByID(foodId);
        if (foods!=null){
            return foods;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> bgFoodGetByName(String foodName) {
        List<Map<String, Object>> foods= foodsDao.bgFoodGetByName(foodName);
        if (foods!=null){
            return foods;
        }
        return null;
    }

    @Override
    public int addOrder(int userId) {
        int updeta = foodsDao.addOrder(userId);
        if (updeta!=0){
            return updeta;
        }
        return 0;
    }

    @Override
    public Foods getById(long id) {
        Foods foods = new Foods();
        TypeDao typeDao = new TypeDaoImpl();
        foods= foodsDao.getById(id);
        long typeId = foods.getType_id();
        Type byId1 = typeDao.getById(typeId);
        foods.setType(byId1);
        return foods;
    }

    /**
     * 查询菜单列表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<Foods> foodList(int pageIndex, int pageSize) {
        List<Foods> foods = foodsDao.foodList(pageIndex, pageSize);
        return foods;
    }


    @Override
    public int getPageCount(int pageSize) {
        int pageCount=0;
        int count = foodsDao.getCount();
        pageCount = (count-1)/pageSize+1;
        return pageCount;
    }

    @Override
    public Orders orderById(int id) {
//        List<Orders> orders = foodsDao.orderById(id);
//        Orders orders1 = new Orders();
//        int sum=0;
//        for (Orders order:orders) {
//            sum+=order.getMoney()*order.getNumber();
//            orders1.setTotal(sum);
//        }
        Orders orders = foodsDao.orderById(id);
        Long aLong = orders.getoNumber();
        List<Orders> orders1 = foodsDao.orderOnNum(aLong);
        Orders orders2= new Orders();
        int sum=0;
        for (Orders order:orders1) {
            sum+=order.getMoney()*order.getNumber();
            orders2.setTotal(sum);
            orders2.setoNumber(order.getoNumber());
        }
        return orders2;
    }

    @Override
    public List<Map<String, Object>> getOrderByUserId(int userId) {
        List<Map<String, Object>> order=foodsDao.getOrderByUserId(userId);
        if (order!=null){
            return order;
        }
        return null;
    }

    public static void main(String[] args) {
        FoodsService foodsDao = new FoodsServiceImpl();
        System.out.println(foodsDao.getById(2));
    }
}
